package com.example.myapplication.SuiPian;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.myapplication.Config.Commom;
import com.example.myapplication.Config.Content;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.Config.User;
import com.example.myapplication.Penson.FeedbackActivity;
import com.example.myapplication.Penson.LonigActivity;
import com.example.myapplication.Penson.ModifyPasswordActivity;
import com.example.myapplication.Penson.PersonalInfoActivity;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.UserPenter;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;


public class PencenFragment extends Fragment {

    private View view;
    private View ll_nickname;
    private View ll_orderlist;
    private View ll_updatapwd;
    private View ll_feedback_but;
    private TextView penter_exit;
    private TextView tv_nickname;
    private SharedPreferences login;
    private SharedPreferences.Editor edit;
    private String TAG = "TAG";
    private Gson gson;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_pencen, container, false);
        initview();
        login();
        //修改密码
        updatepassword();
        //意见反馈
        feedback();
        return view;
    }

    //意见反馈
    private void feedback() {
        ll_feedback_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PencenFragment.this.getContext(), FeedbackActivity.class);
                PencenFragment.this.getContext().startActivity(intent);
            }
        });
    }

    //
    @Override
    public void onResume() {
        super.onResume();
        if(OKHttpTool.TOKEN!=null){
            getuserPerson();
            penter_exit.setText("退出登录");
        }
    }

    private void login() {
        penter_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("登录".equals(penter_exit.getText())) {
                    Intent intent = new Intent(PencenFragment.this.getContext(), LonigActivity.class);
                    PencenFragment.this.getActivity().startActivity(intent);
                }else if("退出登录".equals(penter_exit.getText().toString())){
                    OKHttpTool.TOKEN =null;
                    penter_exit.setText("登录");
                    getuserPerson();
                    edit.putString("token","");
                    edit.apply();
                }
            }
        });
    }


    //修改密码
    private void updatepassword() {
        ll_updatapwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PencenFragment.this.getContext(), ModifyPasswordActivity.class);
                PencenFragment.this.getContext().startActivity(intent);
            }
        });
    }



    private void getuserPerson() {
        OKHttpTool.doNetRequest("prod-api/api/common/user/getInfo",
                OKHttpTool.GET,
                UserPenter.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        getActivity().runOnUiThread((new Runnable() {
                            @Override
                            public void run() {
                                UserPenter userPenter = (UserPenter) obj;
                                if (userPenter.getCode() == 200) {
                                    tv_nickname.setText(userPenter.getUser().getNickName());
                                    binsaveuser(userPenter);
                                }else {
                                    tv_nickname.setText("未登录");
                                }
                            }
                        }));
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }


    //获取个人信息
    private void binsaveuser( UserPenter userPenter) {
        ll_nickname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PencenFragment.this.getContext(), PersonalInfoActivity.class);
                String json = gson.toJson(userPenter);
                intent.putExtra(Commom.USERPENTER, json);
                PencenFragment.this.getContext().startActivity(intent);
            }
        });
    }

    private void initview() {
        tv_nickname = view.findViewById(R.id.tv_nickname);
        ll_nickname = view.findViewById(R.id.ll_nickname);
        ll_orderlist = view.findViewById(R.id.ll_orderlist);
        ll_updatapwd = view.findViewById(R.id.ll_updatapwd);
        ll_feedback_but = view.findViewById(R.id.ll_feedback_but);
        penter_exit = view.findViewById(R.id.penter_exit);
        gson = new Gson();



        login = getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
        edit = login.edit();

    }

}